package com.situ.staff.project.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.situ.staff.project.model.*;
import com.situ.staff.util.FmtEmpty;
import com.situ.staff.util.JDBCUtil;
import java.sql.Timestamp;

public class ProjectDao {

	/**
	 * 添加
	 * 
	 * @param model
	 * @return
	 * @throws SQLException
	 */
	public int insert(ProjectModel model) throws SQLException {
		String sql = "insert into project (code,name,time) values(?,?,?)";
		return JDBCUtil.update(sql, model.getCode(), model.getName(), model.getTime());
	}

	/**
	 * 通过号码删除
	 * 
	 * @param model
	 * @return
	 * @throws SQLException
	 */
	public int deleteByCode(ProjectModel model) throws SQLException {
		String sql = "delete from project where code =?";
		return JDBCUtil.update(sql.toString(), model.getCode());
	}

	/**
	 * 删除
	 * 
	 * @param model
	 * @return
	 * @throws SQLException
	 */
	public int delete(ProjectModel model) throws SQLException {
		StringBuffer sql = new StringBuffer("delete from project");
		List<Object> vals = appendWhere(sql, model);
		return JDBCUtil.update(sql.toString(), vals);
	}

	/**
	 * 更新所有字段
	 * 
	 * @param model
	 * @return
	 * @throws SQLException
	 */
	public int updateAll(ProjectModel model) throws SQLException {
		String sql = "update project set name = ? ,time = ? where code =?";
		return JDBCUtil.update(sql, model.getName(), model.getTime(), model.getCode());
	}

	/**
	 * 更新部分字段
	 * 
	 * @param model
	 * @return
	 * @throws SQLException
	 */
	public int updateActive(ProjectModel model) throws SQLException {
//		String sql = "update project set name = ? and tel = ? where code =?";
		StringBuffer sql = new StringBuffer("update project set id=id ");
		List<Object> vals = new ArrayList<Object>();
		String name = model.getName();
		if (name != null) {
			sql.append(" ,name=?");
			vals.add(name);
		}

		Timestamp time = model.getTime();
		if (time != null) {
			sql.append(" ,time=?");
			vals.add(time);
		}
		sql.append(" where code = ?");
		vals.add(model.getCode());
		return JDBCUtil.update(sql.toString(), vals);
	}

	/**
	 * 查询所有
	 * 
	 * @param model
	 * @return
	 * @throws SQLException
	 */
	public List<ProjectModel> selectList(ProjectModel model) throws SQLException {
		StringBuffer sql = new StringBuffer("select code,name,time from project ");
		List<Object> vals = appendWhere(sql, model);
//		List<String> props = Arrays.asList("code", "name", "tel");
		return JDBCUtil.query(sql.toString(), vals, ProjectModel.class, getFields());
	}

	/**
	 * 条件拼接和sql拼接
	 * 
	 * @param sql
	 * @param model
	 * @return
	 */
	private List<Object> appendWhere(StringBuffer sql, ProjectModel model) {
		// TODO Auto-generated method stub
		List<Object> vals = new ArrayList<Object>();
		sql.append(" where 1=1 ");
		String code = model.getCode();
		if (!FmtEmpty.isEmpty(code)) {
			vals.add(code);
			sql.append(" and code like ?");
		}
		String name = model.getName();
		if (!FmtEmpty.isEmpty(name)) {
			vals.add(name);
			sql.append(" and name like ?");
		}
		Timestamp time = model.getTime();
		if (!FmtEmpty.isEmpty(time)) {
			vals.add(time);
			sql.append(" and time like ?");
		}
		if (model.isPageOn()) {
			sql.append(" limit ?,? ");
			vals.add(model.getRowStart());
			vals.add(model.getPageLimit());
		}
		return vals;
	}

	/**
	 * 查询个体
	 * 
	 * @param model
	 * @return
	 * @throws SQLException
	 */
	public ProjectModel selectModel(ProjectModel model) throws SQLException {
		StringBuffer sql = new StringBuffer("select code,name,time from project");
		List<Object> vals = appendWhere(sql, model);
//		List<String> props = Arrays.asList("code", "name", "tel");
		return JDBCUtil.queryModel(sql.toString(), vals, ProjectModel.class, getFields());
	}

	private Map<String, String> getFields() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("code", "code");
		map.put("name", "name");
		map.put("time", "time");

		return map;

	}

	public Integer selectCount(ProjectModel m0) {
		StringBuffer sql = new StringBuffer("select count(*) from project ");
		List<Object> vals = appendWhere(sql, m0);
		return JDBCUtil.queryInt(sql.toString(), vals);
	}

}
